<template>
  <section>
    <div class="img" v-if="pic" ref="pic">
      点击添加图片
      <input type="file" accept="image/*" @change="resizeImage($event)">
    </div>
    <div class="blank" v-else></div>
    <textarea cols="18" rows="2" placeholder="点击这里
定制你的问题" v-model="question.questionName"></textarea>
    <textarea class="answer" readonly cols="24" rows="3" placeholder="受访者
将会在这里键入他们的答案"></textarea>
  </section>
</template>

<script>
import { checkQuestion } from '@/api/common'
import {uploadImage} from '@/api/image'

export default {
  name: 'survey7',
  data () {
    return {
      question: {
        questionName: '',
        sort: 0,
        type: 7,
        imgUrl: '',
        isSkip: 0,
        optionVoList: []
      }
    }
  },
  props: [
    'pic'
  ],
  methods: {
    resizeImage (e) {
      let file = e.target.files[0]
      window.lrz(file)
      .then(rst => {
        this.$refs.pic.style.backgroundImage = `url(${rst.base64})`
        return rst
      })
      .then(rst => {
        uploadImage(rst.formData).then(rst => {
          let imageUrl = rst.data.data
          this.question.imgUrl = imageUrl
        })
      })
    },
    trans () {
      if (checkQuestion(this.question, 7)) {
        this.$emit('addSurvey', this.question)
        return true
      } else {
        return false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
section {
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: #fff;
}
textarea {
  text-align: center;
  margin: 30px auto;
  font-size: 48px;
  color: #999;
  border: none;
  resize: none;
  &::-webkit-input-placeholder {
    text-align: center;
    color: #999;
  }
}
.img {
  width: 750px;
  height: 750px;
  background-color: #4b539e;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 40px;
  color: #999;
  position: relative;
  background-size: cover;
  input {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
  }
}
.blank {
  width: 100%;
  height: 100px;
}
.answer {
  border: 2px solid #999;
  &::-webkit-input-placeholder {
    text-align: center;
    color: #999;
    line-height: 80px;
  }
}
</style>
